package br.com.iswe.gwt.crud.client;

import java.util.List;

import br.com.iswe.core.entidades.gwt.BeanMap;
import br.com.iswe.core.entidades.gwt.RemoteEntityObject;
import br.com.iswe.gwt.ui.client.Listener;
import br.com.iswe.gwt.ui.client.ModalDialogBox;
import br.com.iswe.gwt.ui.client.QueryInfo;
import br.com.iswe.gwt.ui.client.service.ServiceClientAbstract;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;

public abstract class DetailsPanelAbstract<E extends RemoteEntityObject> extends Composite {

	protected static final String PROPERTY = "property";
	
	@SuppressWarnings("unchecked")
	protected void showSearchPanelAsPopup(
			SearchPanel searchPanel, 
			final QueryInfo queryInfoDetailList, 
			final BeanMap bean, 
			final ServiceClientAbstract<E> manyToManyService, 
			String title,
			final AsyncCallback<E> asyncCallback) {
		final ModalDialogBox modalPanel = new ModalDialogBox();
		//modalPanel.setText(title);
		modalPanel.setWidth("700px");
		modalPanel.setupDialogBox(title );
		modalPanel.setWidget( searchPanel  );
		modalPanel.show();
		
		searchPanel.onSelectListener.add(new Listener<List<BeanMap>>(){
			public void execute(List<BeanMap> selectedItensList) {
				//evento ocorre qdo o usuario fecha o popup
				
				//esconde o popup
				modalPanel.hide();
				
				//prepara o bean para mandar para o servidor
				if(selectedItensList.size() > 0){
					
					String property = bean.getString(PROPERTY);
					//lista
					bean.put(property, selectedItensList);
					
					//salva os novos itens
					manyToManyService.associarManyToMany(bean, queryInfoDetailList, asyncCallback);
				}
				
			}});
	}
	
}
